﻿using RabbitMQ.Client;

namespace EventBus.RabbitMQ
{
    public class RabbitMQConnectionProvider
    {
        public static ConnectionConfig Config
        {
            get
            {
                return new ConnectionConfig
                {
                    HostName = "118.24.105.216:15672",
                    UserName = "wsy",
                    Password = "wsy123"
                };
            }
        }

        /// <summary>
        /// 实例化一个连接
        /// </summary>
        /// <returns></returns>
        public static IConnectionFactory CreateFactory()
        {
            var factory = new ConnectionFactory()
            {
                HostName = Config.HostName,
                DispatchConsumersAsync = true
            };

            if (!string.IsNullOrWhiteSpace(Config.UserName))
            {
                factory.UserName = Config.UserName;
            }

            if (!string.IsNullOrWhiteSpace(Config.Password))
            {
                factory.Password = Config.Password;
            }
            return factory;
        }
    }

    public class ConnectionConfig
    {
        /// <summary>
        /// 服务器地址
        /// </summary>
        public string HostName { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }
    }
}
